package com.barbeque.mall.service;

import com.barbeque.mall.bean.BasePageInfo;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.bean.system.MarketLog;
import com.barbeque.mall.bean.system.MarketLogExample;
import com.barbeque.mall.mapper.MarketLogMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 苏东宇
 * @since 2022/07/18 09:29
 */
@Service
public class SysLogServiceImpl implements SysLogService {

    @Autowired
    MarketLogMapper marketLogMapper;

    /**
     * 创建一个日志信息插入数据库中
 * @param request
 * @param joinPoint
 * @param marketLog
     * @return int
     * @author 苏东宇
     * @since 2022/07/18 14:11
     */
    @Override
    public int createLog(HttpServletRequest request, ProceedingJoinPoint joinPoint, MarketLog marketLog) {
        marketLogMapper.insert(marketLog);
        return 0;
    }


    /**
     * 查询数据库中的信息 ，删除位不为1的，并且如果adminUsername不为空的话，按照adminUserName进行模糊查询
     *
 * @param basePageInfo
 * @param adminUsername
     * @return com.barbeque.mall.bean.param.CommonData
     * @author 苏东宇
     * @since 2022/07/18 14:12
     */
    @Override
    public CommonData getLogList(BasePageInfo basePageInfo, String adminUsername) {
        PageHelper.startPage(basePageInfo.getPage(),basePageInfo.getLimit());

        MarketLogExample marketLogExample = new MarketLogExample();
        MarketLogExample.Criteria criteria = marketLogExample.createCriteria();
        criteria.andDeletedEqualTo(false);

        if (adminUsername != null){
            criteria.andAdminLike("%" + adminUsername + "%");
        }

        List<MarketLog> marketLogs = marketLogMapper.selectByExample(marketLogExample);
        PageInfo<MarketLog> marketLogPageInfo = new PageInfo<>(marketLogs);


        return CommonData.data(marketLogPageInfo);
    }
}
